package com.hysoft.common.result;

import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;

/**
 * <h1>请求响应包装类</h1>
 *
 * @author 王天瑞
 * @since 1.0
 * @version 1.0
 * @param <T> 返回数据类型
 */
@Schema(name = "统一响应结果", description = "统一响应结果")
public class Result<T> implements Serializable {
    /**
     * <h2>请求是否成功</h2>
     */
    @Schema(name = "请求是否成功", description = "请求是否成功")
    private boolean ok;
    /**
     * <h2>请求响应代码</h2>
     */
    @Schema(name = "请求响应代码", description = "请求响应代码")
    private String code;
    /**
     * <h2>请求响应数据</h2>
     */
    @Schema(name = "请求响应代码", description = "请求响应结果")
    private T data;
    /**
     * <h2>请求响应说明</h2>
     */
    @Schema(name = "请求响应说明", description = "请求响应说明")
    private String message;

    /**
     * <h2>成功无返回请求</h2>
     *
     * @return 请求响应结果
     * @param <T> 请求返回类型
     */
    public static <T> Result<T> ok() {
        return ok(null);
    }

    /**
     * <h2>成功有返回请求</h2>
     *
     * @param data 请求返回数据
     * @return 请求响应结果
     * @param <T> 请求返回类型
     */
    public static <T> Result<T> ok(T data) {
        ResultCode rce = ResultCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            rce = ResultCode.SYSTEM_EXECUTION_ERROR;
        }
        return result(rce, data);
    }

    public static <T> Result<T> failed() {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMessage(), null);
    }

    public static <T> Result<T> failed(String msg) {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static <T> Result<T> failed(IResultCode resultCode, String msg) {
        return result(resultCode.getCode(), msg, null);
    }

    private static <T> Result<T> result(IResultCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMessage(), data);
    }

    private static <T> Result<T> result(String code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }

    public static boolean isSuccess(Result<?> result) {
        return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
    }

    public boolean isOk() {
        return ResultCode.SUCCESS.getCode().equals(this.getCode());
    }

    public void setOk(boolean ok) {
        this.ok = ok;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
